import java.util.Arrays;

public class MoveZeroes {
    public static void moveZeroes(int []nums){
        int n=nums.length;
        int right=0;
        int left=0;
        while(right<n){
            if(nums[right]!=0){//右指针一直要指向0，若为非零数，则与左指针交换
                swap(nums,right,left);
                left++;//左指针指向排列好的数组的位置的尾部
            }
            right++;
        }
    }
    public static void swap(int []nums,int right,int left){
        int temp=nums[right];
        nums[right]=nums[left];
        nums[left]=temp;
    }

    public static void main(String[] args) {
        int []nums1={0,12,3,4,0,32,0,2};
        int []nums2={1,3,3,4,5,6,0,9,0,0,0};
        int []nums3={0,0,0,4};
        System.out.println(Arrays.toString(nums1));
        System.out.println(Arrays.toString(nums2));
        System.out.println(Arrays.toString(nums3));
        System.out.println("调用函数后");
        moveZeroes(nums1);
        moveZeroes(nums2);
        moveZeroes(nums3);
        System.out.println(Arrays.toString(nums1));
        System.out.println(Arrays.toString(nums2));
        System.out.println(Arrays.toString(nums3));
    }
}
